package com.designoutput.ui
{
	import com.designoutput.event.CardEvent;
	import com.designoutput.event.CardLayerEvent;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.utils.Globals;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class FacesToolBarItem extends ControlBase
	{
		private var _literal:Literal;
		private var _layer:CardFace;
		private var _layerChanged:Boolean = false;
		
		private var _viewIcon:Sprite;
		private var _printIcon:Sprite;
		
		private var _layerVisible:Boolean = false;
		
		[Embed(source="/temporaryAssets/view-icon.gif")]
		public var viewIconClass:Class;
		
		[Embed(source="/temporaryAssets/print-icon.gif")]
		public var printIconClass:Class;
		
		public function get layer():CardFace {
			return _layer;
		}
		
		public function set layer(value:CardFace):void {
			if(_layer != null){
				_layer.removeEventListener(CardLayerEvent.VIEWING_CHANGED, layer_viewingChanged, false);
			}
			
			_layer = value;
			
			if(_layer != null){
				_layer.addEventListener(CardLayerEvent.VIEWING_CHANGED, layer_viewingChanged, false, 0, true);
			}
			
			_layerChanged = true;
			invalidateProperties();
		}
		
		private function layer_viewingChanged(e:Event):void {
			invalidateDisplay();
		}
		
		override protected function initialize():void {
			super.initialize();
			
			_width = 130;
			_height = 20;
			
			_literal = new Literal();
			addChild(_literal);
			
			_viewIcon = new Sprite();
			_viewIcon.addChild(new viewIconClass());
			_viewIcon.buttonMode = true;
			_viewIcon.addEventListener(MouseEvent.CLICK, _viewIcon_clickHandler, false, 0, true);
			addChild(_viewIcon);
			
			_printIcon = new Sprite();
			_printIcon.addChild(new printIconClass());
			_printIcon.buttonMode = true;
			_printIcon.addEventListener(MouseEvent.CLICK, _printIcon_clickHandler, false, 0, true);
			addChild(_printIcon);
		}
		
		private function _viewIcon_clickHandler(e:MouseEvent):void {
			if(e.shiftKey){
				toggleView();
			} else {
				while(Globals.designoutputCard.canvas.numEditors > 0)
					Globals.designoutputCard.canvas.removeEditorAt(0);
				 
				toggleView();
			}
			invalidateDisplay();
		}
		
		private function _printIcon_clickHandler(e:MouseEvent):void {
			_layer.print = !_layer.print;
			invalidateDisplay();
			
			if(!_layer.print && _layer.viewing)
				toggleView();
		}
		
		private function toggleView():void {
			var editor:CardFaceEditor;
			
			if(_layer.viewing){
				editor = Globals.designoutputCard.card.getEditorForLayer(_layer);
				Globals.designoutputCard.canvas.removeEditor(editor);	
			} else {
				editor = Globals.designoutputCard.card.getEditorForLayer(_layer);
				Globals.designoutputCard.canvas.addEditor(editor);
			}
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			
			if(_layerChanged){
				_layerChanged = false;
				_literal.text = _layer.name;
				invalidateDisplay();
			}
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
						
			_printIcon.x = _width - _printIcon.width - 2;
			_printIcon.y = 3;
			_printIcon.alpha = _layer.print ? 1 : 0.3;
			
			_viewIcon.x = 4;
			_viewIcon.y = 2;
			
			_viewIcon.alpha = _layer.viewing ? 1 : 0.3;
			_viewIcon.visible = _layer.print;
			
			_literal.x = _viewIcon.x + _viewIcon.width + 2;
			_literal.width = _width - _literal.x;
			
			graphics.lineStyle(1, 0);
			graphics.beginFill(0xFFFFFF);
			graphics.drawRect(0, 0, _width, _height);
		}
	}
}